

	var scene = new THREE.Scene();
    //创建照相机，这是一个透视投影相机
    var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
    //将照相机加入到场景中
    scene.add(camera);
    //创建渲染器

    var renderer = new THREE.WebGLRenderer({ antialias: true });//抗锯齿渲染
    //设置渲染器的背景色
    // renderer.setClearColor(new THREE.Color(0x333333, 1.0));
    //设置渲染器的大小
    renderer.setSize(window.innerWidth, window.innerHeight);
    //设置渲染器是否显示阴影
    renderer.shadowMap.enabled = true;

    //设置照相机的位置
    camera.position.x = 0;
    camera.position.y = 0;
    camera.position.z = 100;
    //设置照相机的观察中心
    var op = new THREE.Vector3(0,0,0);
    camera.lookAt(op);
    // camera.lookAt(scene.position);

    var controls = new THREE.OrbitControls( camera, renderer.domElement );

    function MakeCube(x,y,z,a){
    	// var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x999999});
    	var mats = [];
    	mats.push(new THREE.MeshLambertMaterial({color: 0xff0000}));
    	mats.push(new THREE.MeshLambertMaterial({color: 0xff00ff}));
    	mats.push(new THREE.MeshLambertMaterial({color: 0x00ffff}));
    	mats.push(new THREE.MeshLambertMaterial({color: 0x00ff00}));
    	mats.push(new THREE.MeshLambertMaterial({color: 0x0000ff}));
    	mats.push(new THREE.MeshLambertMaterial({color: 0xffff00}));
    	var faceMaterial = new THREE.MultiMaterial( mats );
	    var cubeGeometry = new THREE.BoxGeometry(a, a, a);
	    var cube = new THREE.Mesh(cubeGeometry, faceMaterial);
	    cube.castShadow = true;
	    cube.position.x = x;
	    cube.position.y = y;
	    cube.position.z = z;
	    return cube;
    }

    function Sierpinski(x,y,z,a,n){
    	if(n==1){
			Sierpinski1(x,y,z,a);
    	}else{
    		for(var i=0;i<3;i++)
				for(var j=0;j<3;j++)
					for(var k=0;k<3;k++){
						if(i*j==1 || j*k==1 || k*i==1){

						}else{
							Sierpinski(x-(a/3)+(i*a/3),y-(a/3)+(j*a/3),z-(a/3)+(k*a/3),a/3,n-1);
						}
					}
    	}
    }

    var Sie = [];
    function Sierpinski1(x,y,z,a){  //1维的门格海绵体
    	for(var i=0;i<3;i++)
			for(var j=0;j<3;j++)
				for(var k=0;k<3;k++){
					if(i*j==1 || j*k==1 || k*i==1){

					}else{
						var cube=MakeCube(x-(a/3)+(i*a/3),y-(a/3)+(j*a/3),z-(a/3)+(k*a/3),a/3);
                        scene.add(cube);
                        Sie.push(cube);
					}
				}
    }

    // MakeCube(0,0,0,50);
    Sierpinski(0,0,0,50,2);

    // MakeCube(0,0,0,5);
    // Sierpinski1(0,0,0,5);

    //三维坐标轴
    function ax(){
        var geometryx = new THREE.Geometry();
        var geometryy = new THREE.Geometry();
        var geometryz = new THREE.Geometry();

        var p1 = new THREE.Vector3(-100, 0, 0);
        var p2 = new THREE.Vector3(100, 0, 0);
        var p3 = new THREE.Vector3(0,-100,0);
        var p4 = new THREE.Vector3(0,100,0);
        var p5 = new THREE.Vector3(0,0,-100);
        var p6 = new THREE.Vector3(0,0,100);

        geometryx.vertices.push(p1);
        geometryx.vertices.push(p2);
        geometryy.vertices.push(p3);
        geometryy.vertices.push(p4);
        geometryz.vertices.push(p5);
        geometryz.vertices.push(p6);

        var linex = new THREE.Line( geometryx, new
            THREE.LineBasicMaterial( { color: 0xFF0000, opacity: 1.0}));//红色x轴
        var liney = new THREE.Line( geometryy, new
            THREE.LineBasicMaterial( { color: 0x00FF00, opacity: 1.0}));//黄色y轴
        var linez = new THREE.Line( geometryz, new
            THREE.LineBasicMaterial( { color: 0x0000FF, opacity: 1.0}));//蓝色z轴

        scene.add( linex );
        scene.add( liney );
        scene.add( linez );
    }
    ax();


    // 添加环境光源
    // var ambientLight = new THREE.AmbientLight(0x0f0f0f);
    // //将环境光源加入到场景中
    // scene.add(ambientLight);

    //创建一个聚光光源
    function addLight(){
        var spotLight1 = new THREE.SpotLight(0xffffff);
        spotLight1.position.set(-80, 0, 0);
        spotLight1.castShadow = true;
        scene.add(spotLight1);

        var spotLight2 = new THREE.SpotLight(0xffffff);
        spotLight2.position.set(80,0,0);
        spotLight2.castShadow = true;
        scene.add(spotLight2);

        var spotLight3 = new THREE.SpotLight(0xffffff);
        spotLight3.position.set(0,80,0);
        spotLight3.castShadow = true;
        scene.add(spotLight3);

        var spotLight4 = new THREE.SpotLight(0xffffff);
        spotLight4.position.set(0,-80,0);
        spotLight4.castShadow = true;
        scene.add(spotLight4);

        var spotLight5 = new THREE.SpotLight(0xffffff);
        spotLight5.position.set(0,0,80);
        spotLight5.castShadow = true;
        scene.add(spotLight5);

        var spotLight6 = new THREE.SpotLight(0xffffff);
        spotLight6.position.set(0,0,-80);
        spotLight6.castShadow = true;
        scene.add(spotLight6);
    }
    addLight();

    //将渲染输出到html元素中
    document.body.appendChild(renderer.domElement);

    //调用渲染场景
    renderer.render(scene, camera);
    animate();
    // renderScene();

    function animate()
    {
        requestAnimationFrame( animate );
        render();
        update();
    }

    function update()
    {
        controls.update();
    }

    function render()
    {
        renderer.render( scene, camera );
    }

    var guiControls = function() {  //控制类，用来控制参数
        this.message = 'n>3,内存小,显示慢';
        this.n = 2;
        this.redraw = function() { //重绘函数，当gui参数改变时调用
            for(var i=0;i<Sie.length;i++)
                scene.remove(Sie[i]);
            Sierpinski(0,0,0,50,this.n);
        }
    }
    var con = new guiControls();
    var gui = new dat.GUI();
    var conn = gui.add(con,'n',1,3).step(1);
    conn.onChange(function() {
        con.redraw();
    });
    gui.add(con, 'message');

